package com.crl.ssc.dic.controller;

import com.cars.util.global.GlobalOperaCode;
import com.cars.util.global.GlobalReturnCode;
import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.string.StringUtil;
import com.cars.util.validation.UpdateValidGroup;
import com.crl.ssc.dic.dto.DicTestDTO;
import com.crl.ssc.dic.service.DicTestService;
import com.crl.ssc.dic.vo.DicTestSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @description: 字典管理-测试数据表API
 * @author:  孔垂云
 * @version: 1.0
 * @date: 2020-08-04 00:10:36
 */
@RestController
@RequestMapping("/dic/test")
@Api(tags = "字典管理-测试数据表")
public class DicTestController {

    @Autowired
    private DicTestService dicTestService;

    /**
     * 测试数据表列表
     *
     * @param dicTestSearchVO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "测试数据表列表")
    public JsonResult list(@RequestBody DicTestSearchVO dicTestSearchVO) {
        List<DicTestDTO> list = dicTestService.list(dicTestSearchVO);
        return JsonResultUtil.ok(list);
    }

    /**
     * 新增测试数据表
     *
     * @param dicTestDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增测试数据表")
    public JsonResult add(@RequestBody @Valid DicTestDTO dicTestDTO) {
        int flag = dicTestService.add(dicTestDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }
    /**
     * 修改测试数据表
     *
     * @param dicTestDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改测试数据表")
    public JsonResult update(@Validated({UpdateValidGroup.class}) @RequestBody DicTestDTO dicTestDTO) {
        int flag = dicTestService.update(dicTestDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }
    /**
     * 删除测试数据表
     *
     * @param map
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除测试数据表")
    public JsonResult delete(@RequestBody Map<String, String> map) {
        String id = map.get("id");
        if (StringUtil.isNullOrEmpty(id)) {
            JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR);
        }
        int flag = dicTestService.delete(Long.parseLong(id));

        if (flag == GlobalOperaCode.OPERA_SUCCESS) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }
    /**
     * 获取单个测试数据表
     *
     * @param map
     * @return
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取单个测试数据表")
    public JsonResult get(@RequestBody Map<String, String> map) {
        String id = map.get("id");
        if (StringUtil.isNullOrEmpty(id)) {
            JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR);
        }
        DicTestDTO dicTestDTO = dicTestService.get(Long.parseLong(id));
        return JsonResultUtil.ok(dicTestDTO);
    }
}
